<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Polizas extends CI_Controller {

    public function main() {
        $this->load->helper('form');
        $this->load->library('pagination');
        $offset = $this->uri->segment(3);
        $limit = PAGINATOR_LIMIT;
        $this->db->limit($limit, $offset);
        $query = $this->db->get('polizas');
        $config['base_url'] = site_url() . '/polizas/main/';
        $config['total_rows'] = $this->db->count_all('polizas');
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);
        $paginator = $this->pagination->create_links();
        $data = array(
            'query' => $query,
            'paginator' => $paginator
        );
        $this->load->library('AucarAssets');
        $assets = new AucarAssets;
        $data['assets'] = $assets->print_assets();

        $this->load->library('AucarMenu');
        $menu = new AucarMenu;
        $data['menu'] = $menu->print_menu();
        $this->load->view('polizas/home', $data);
    }

    public function edit() {
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->library('pagination');
        $id = $this->uri->segment(3);
        $query = $this->db->get_where('polizas', array('id' => $id));
        $obj = $query->row();
        $data = array(
            'obj' => $obj
        );
        $this->load->library('AucarAssets');
        $assets = new AucarAssets;
        $data['assets'] = $assets->print_assets();
        $this->load->library('AucarMenu');
        $menu = new AucarMenu;
        $data['menu'] = $menu->print_menu();
        $data['error'] = '';
        $this->load->view('polizas/form', $data);
    }

    public function add() {
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->library('pagination');
        $id = $this->uri->segment(3);

        $data = array(
            'obj' => null
        );
        $this->load->library('AucarAssets');
        $assets = new AucarAssets;
        $data['assets'] = $assets->print_assets();
        $this->load->library('AucarMenu');
        $menu = new AucarMenu;
        $data['menu'] = $menu->print_menu();
        $this->load->view('polizas/form', $data);
    }

    public function save() {
        $obj = $this->input->post();
        $vitalicio = $this->input->post('vitalicio');
        $obj['vitalicio'] = $vitalicio[0];
        if (empty($obj['id'])) {
            $this->db->insert('polizas', $obj);
            redirect('/polizas/main/');
        } else {
            $this->db->where('id', $obj['id']);
            array_shift($obj);
            $this->db->update('polizas', $obj);
            redirect('/polizas/main/');
        }
    }

    public function search() {
        $this->load->helper('form');
        $this->load->library('pagination');
        $offset = $this->uri->segment(3);
        $search = $this->input->post('search');
        $limit = PAGINATOR_LIMIT;
        $this->db->limit($limit, $offset);
        $array = array('npoliza' => $search);
        $this->db->or_like($array);
        $query = $this->db->get('polizas');
        $config['base_url'] = site_url() . '/polizas/search/';
        $config['total_rows'] = $this->db->count_all('polizas');
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);
        $paginator = $this->pagination->create_links();
        $data = array(
            'query' => $query,
            'paginator' => $paginator
        );
        $this->load->library('AucarAssets');
        $assets = new AucarAssets;
        $data['assets'] = $assets->print_assets();
        $this->load->library('AucarMenu');
        $menu = new AucarMenu;
        $data['menu'] = $menu->print_menu();
        $this->load->view('polizas/home', $data);
    }

    function upload_file() {

        $polizas_id = $this->input->post('polizas_id');
        mkdir('./uploads/' . $obj['polizas_id'], 0777);
        $config['upload_path'] = './uploads/' . $polizas_id . '/';
        $config['allowed_types'] = 'gif|jpg|png|pdf';
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            die(var_dump($error));
        } else {
            $data = array('upload_data' => $this->upload->data());

            $obj['ruta'] = $data['upload_data']['full_path'];

            $this->db->insert('archivos', $obj);

            $poliza_has_obj['archivos_id'] = $this->db->insert_id();
            $poliza_has_obj['polizas_id'] = $polizas_id;
            $this->db->insert('archivos_has_polizas', $poliza_has_obj);
            redirect('/polizas/edit/' . $poliza_has_obj['polizas_id']);
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/polizas.php */